var express = require('express'),
  // swig = require('swig-templates'),
  request = require('request'),
  // wx = require('../base/wx'),
  // sha1 = require('node-sha1'),
  // crypto = require('crypto'),
  router = express.Router();

/*
router.get('/', function(req, res, next) {
  wx.goAccessToken(req, res, function() {
    wx.getToken(req, function() {
      request.get({
        url: config.resourcePath.apiRoot + '/index/index/edit?user_id=' + (req.cookies.user_id ? req.cookies.user_id : '0') + '&product_id=' + req.query.id + "&type=" + req.query.type
      }, function(error, response, body) {
        try {
          var result = JSON.parse(body);
          console.log(result);
          if (result.code == 200) {
            var rundData = {
              message: result.message,
              type: req.query.type
            };
            res.render('page/edit', rundData);
          }
        } catch (e) {
          var err = new Error('Not Found');
          err.status = 500;
          next(err);
          return;
        }
      });
    });
  });
});
*/

router.get('/', function(req, res, next) {
  if (parseInt(req.cookies.user_id) > 0) {
    req.app.sync(function* (){
      let requestSync = req.app.async2sync(request.get);
      let result = yield requestSync({
        url: config.resourcePath.apiRoot + '/index/index/edit?user_id=' + req.cookies.user_id + '&product_id=' + req.query.id + "&type=" + req.query.type,
        json: true
      });
      console.log(JSON.stringify(result));
      /*
      {
          "code": 200,
          "kind": "mobile",
          "message": {
              "template": {
                  "type": "diy",
                  "name": "iP6/6s plus-光面",
                  "basemap": "/uploads/basemap/20170720/dfc076474a0f12a3a3e79ac016c5c520.png",
                  "widthMm": "98.98",
                  "heightMm": "164.93",
                  "width": 1220,
                  "height": 2010,
                  "offset_x": 490,
                  "offset_y": 95,
                  "image": "",
                  "if_no_image": {
                      "bg_width": 2200,
                      "bg_height": 2200,
                      "bg_color": "FFFFFF"
                  },
                  "left_image": "",
                  "right_image": "",
                  "up_image": "",
                  "down_image": "",
                  "origin_price": "15.00",
                  "preferential_price": "30.00"
              },
              "chartlet": [{
                  "type": "默认",
                  "content": [{
                      "chartlet_id": 2,
                      "name": "飞舞",
                      "type_id": 2,
                      "url": "/uploads/chartlet/20170713/c59fb2986ad981c3bdc0f8f11b9c86d1.png",
                      "type": "full"
                  }, {
                      "chartlet_id": 8,
                      "name": "tatoo",
                      "type_id": 2,
                      "url": "/uploads/chartlet/20170725/2a8597fb932663b83eca4ea284dee2bf.png",
                      "type": "full"
                  }, {
                      "chartlet_id": 10,
                      "name": "仙人掌",
                      "type_id": 2,
                      "url": "/uploads/chartlet/20170725/eb5460b175de4d949a2ce85b76046165.jpg",
                      "type": "full"
                  }, {
                      "chartlet_id": 11,
                      "name": "字母",
                      "type_id": 2,
                      "url": "/uploads/chartlet/20170725/44cb15ae60a9d8234138f067eed16a0e.jpg",
                      "type": "full"
                  }, {
                      "chartlet_id": 12,
                      "name": "扶桑花",
                      "type_id": 2,
                      "url": "/uploads/chartlet/20170725/750988b001465a9a3b3da212f002fa6f.jpg",
                      "type": "full"
                  }, {
                      "chartlet_id": 13,
                      "name": "火烈鸟",
                      "type_id": 2,
                      "url": "/uploads/chartlet/20170725/1199896735357cdad7bb24439969e016.jpg",
                      "type": "full"
                  }]
              }],
              "mask": [{
                  "type": "推荐",
                  "content": [{
                      "mask_id": 14,
                      "name": "萌",
                      "type_id": 1,
                      "url": "/uploads/mask/20180117/250fb27dfa08a5585c50efb72c9ce720.png"
                  }, {
                      "mask_id": 15,
                      "name": "火烈鸟",
                      "type_id": 1,
                      "url": "/uploads/mask/20180117/aa779a8ca5db6905036be1a7b349b04d.png"
                  }, {
                      "mask_id": 16,
                      "name": "仙人掌",
                      "type_id": 1,
                      "url": "/uploads/mask/20180317/bcf104030a117229591855065ea2d474.png"
                  }, {
                      "mask_id": 18,
                      "name": "宇航员",
                      "type_id": 1,
                      "url": "/uploads/mask/20180322/bc764ae51cefc1ef2407c5b7aa20b8c3.png"
                  }, {
                      "mask_id": 19,
                      "name": "怪物",
                      "type_id": 1,
                      "url": "/uploads/mask/20180322/3f74aa30473bf518d10e289e08d1592c.png"
                  }]
              }]
          },
          "imsg": "返回数据成功",
          "isAction": 0
      }

       */
      console.log(typeof result);
      // console.log(typeof result.index1);
      // console.log(typeof result.index2);
      if (result && result.code == 200) {
          var rundData = {
              message: result.message,
              type: req.query.type
            };
            res.render('page/edit', rundData);
        }
    }, 2);
  } else {
    // res.cookie('user_id', false, {
    //   maxAge: config.cookieTime,
    //   httpOnly: false
    // });
    res.clearCookie('user_id');
    res.redirect("/");

    // 上面的else 改成一个中间件统一处理cookie无效时的操作
    // next();
  }
});

module.exports = router;